package com.frame.service.base.biz.convert.bak.dict;

import com.frame.common.component.dict.IDict;
import com.frame.convert.core.AbstractConverter;
import com.frame.convert.core.ConvertContext;
import com.frame.convert.core.ConvertListContext;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/*
 * @author zhangyan
 * @date 2021-04-24 20:46:50
 **/
public class DictArrConverter extends AbstractConverter<DictArr, String,List<? extends IDict>> {

    @Override
    public List<? extends IDict> convert(ConvertContext<DictArr, String> context) {
        String sourceValue = context.getSourceValue();
        DictArr annotation = context.getConverterAnnotation();
        if (StringUtils.isNotBlank(sourceValue)){
            String[] split = sourceValue.split(",");
            List<IDict> resList=new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                String code = split[i];
                Class<? extends IDict> dictClass = annotation.dictClass();
                IDict dict = IDict.get(dictClass, code);
                resList.add(dict);
            }
            return resList;
        }
        return null;
    }

    @Override
    public Function<ConvertContext<DictArr, String>, List<? extends IDict>> convertList(ConvertListContext<DictArr, String> convertListContext) {
        return this::convert;
    }
}
